#define _CRT_SECURE_NO_WARNINGS 1
class Solution {
public:
    ListNode* swapPairs(ListNode* head) {
        if (head == nullptr)
            return nullptr;
        if (head->next == nullptr)
            return head;
        ListNode* cur = head;
        ListNode* prev = nullptr;
        ListNode* newhead = head->next;
        while (cur && cur->next) {
            ListNode* next = cur->next;

            cur->next = next->next;
            next->next = cur;
            if (prev)
                prev->next = next;
            prev = cur;
            cur = cur->next;
        }
        return newhead;
    }
};